<?php
namespace Models\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;

class RatingTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function postRating($data,$review_id)
    {
		$data = array(
			'rating' 		=> $data['rating'], 			
			'user_id'  	 	=> $data['user_id'], 	
			'review_id' 	=> $review_id,   
			'added_date'	=> date('Y-m-d H:i:s'),
			'updated_date'	=> date('Y-m-d H:i:s'),			
			'status'	 	=> 0, 				
		);	
		$insertresult=$this->tableGateway->insert($data);	
		return $insertresult;	
    }
	public function getRatingsofShops($shop_id,$rating){
		$select = $this->tableGateway->getSql()->select();
		$select->join('reviews', new Expression('rating.review_id=reviews.review_id'),array('*'),'left');
		$select->join('order_delivery_details', new Expression('order_delivery_details.user_medicine_order_id=reviews.tracking_id'),array('shop_id'),'left');
		$select->where('order_delivery_details.shop_id="'.$shop_id.'"');
		// $select->where('order_delivery_details.shop_order_status="3"');
		if($rating==1){
			$select->where('rating.rating="'.$rating.'"');
		}else if($rating==2){
			$select->where('rating.rating="'.$rating.'"');
		}else if($rating==3){
			$select->where('rating.rating="'.$rating.'"');
		}else if($rating==4){
			$select->where('rating.rating="'.$rating.'"');
		}else if($rating==5){
			$select->where('rating.rating="'.$rating.'"');
		}		
		$select->group('rating.rating_id');
		$select->order('rating.rating_id DESC');
		$resultSet = $this->tableGateway->selectWith($select);		
		$row=$resultSet->count();
		return $row;
	}
}